package com.voxel.sense.infomsg.module.task.send.userlist;

import android.text.TextUtils;
import android.view.View;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.data.bean.ConditionBean;
import com.voxel.sense.infomsg.utils.GlideUtils;
import com.voxel.sense.infomsg.widget.adapter.AbsPickRecyclerAdapter;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;
import cyy.example.com.facotry.constants.Constants;

/**
 * 菜单筛选栏的适配器
 * Created by ChenYiYao on 2018/8/10.
 */
public class UserPickRecyclerAdapter extends AbsPickRecyclerAdapter implements Filterable {

    private List<ConditionBean> mSourceList; //数据源


    public UserPickRecyclerAdapter(List<ConditionBean> sourceList) {
        mSourceList = sourceList;
        mDataList.addAll(sourceList);
    }

    @Override
    protected int getItemViewType(int position, ConditionBean conditionBean) {
        if (Constants.HEAD_ITEM.equals(conditionBean.getType())) {
            return R.layout.item_recycler_textview;
        } else {
            return R.layout.item_recyler_select_tip;
        }
    }

    @Override
    protected RecyclerAdapter.ViewHolder<ConditionBean> onCreateViewHolder(View root, int viewType) {
        ViewHolder holder;
        if (viewType == R.layout.item_recycler_textview) {
            holder = new VHeadHolder(root);
        } else {
            holder = new VHolder(root);
        }
        return holder;
    }

    @Override
    public Filter getFilter() {
        return new Filter() {
            /**
             * 该方法是在子线程中，所以数据的筛选计算应放在这里。
             * @param constraint
             * @return
             */
            @Override
            protected FilterResults performFiltering(CharSequence constraint) {
                FilterResults results = new FilterResults();

                final ArrayList<ConditionBean> newValues = new ArrayList<ConditionBean>();
                if (TextUtils.isEmpty(constraint)) {
                    results.count = mSourceList.size();
                    results.values = mSourceList;
                } else {
                    for (int i = 0; i < mSourceList.size(); i++) {
                        ConditionBean cdBean = mSourceList.get(i);
                        //筛选包含筛选文件的数据，且排除掉head
                        if (!Constants.HEAD_ITEM.equals(cdBean.getType()) &&
                                (TextUtils.isEmpty(cdBean.getDesc()) || cdBean.getDesc().contains(constraint))) {
                            newValues.add(cdBean);
                        }
                    }
                    results.count = newValues.size();
                    results.values = newValues;
                }
                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                //得到数据结果，刷新列表
                replace((Collection<ConditionBean>) results.values);
            }
        };
    }

    @Override
    protected boolean handlerSatu(ConditionBean conditionBean) {
        return !Constants.HEAD_ITEM.equals(conditionBean.getType());
    }


    public static class VHolder extends RecyclerAdapter.ViewHolder<ConditionBean> {
        @BindView(R.id.tv_desc)
        TextView tv_desc;
        @BindView(R.id.iv_select_icon)
        ImageView iv_select_icon;

        public VHolder(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(ConditionBean bean) {
            tv_desc.setText(bean.getDesc());
            if (bean.isSelect()) {
                GlideUtils.GlideLoadPicNoDefault(iv_select_icon.getContext(), R.mipmap.map_tip_select_icon, iv_select_icon);
            } else {
                GlideUtils.GlideLoadPicNoDefault(iv_select_icon.getContext(), R.mipmap.map_tip_unselect_icon, iv_select_icon);
            }
        }
    }

    public static class VHeadHolder extends RecyclerAdapter.ViewHolder<ConditionBean> {
        @BindView(R.id.iv_desc_icon)
        TextView iv_desc_icon;

        public VHeadHolder(View itemView) {
            super(itemView);
        }

        @Override
        protected void onBind(ConditionBean bean) {
            iv_desc_icon.setText(bean.getDesc());
        }
    }


    public List<ConditionBean> getSourceList() {
        return mSourceList;
    }

    public void setSourceList(List<ConditionBean> sourceList) {
        mSourceList = sourceList;
    }
}

